
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32; // 大小为四字节共32位


// 延时函数
void delay(volatile u32 n);

int main()
{
// 两种改变对应位的值的方法
#if 0
	*(u32 *)0x4002 1018 |= 1 << 2; //

	*(u32 *)0x40010804 &= ~0xf;
	*(u32 *)0x40010804 |= 0x1;

	while (1)
	{
		*(u32 *)0x4001080C &= ~(0x1 << 8); // 蜂鸣器开
		delay(1000);
		*(u32 *)0x4001080C |= (0x1 << 8); // 关
		delay(1000);
	}

#endif
#if 0
	*(u32 *)0x4002 1018 |= 1 << 3; // 开启PB

	*(u32 *)0x40010C00 &= ~0x00f000ff; // 将对应的开关设置制0
	*(u32 *)0x40010C00 |= 0x00100011;  // 将GPIO0，1，5对应的位置设置为推挽输出
	int blue = 10, red = 0, green = 0, i = 0;
	int addb = 1, addr = 1, addg = 0;
	while (1)
	{
		for (i = 0; i < 1000; i++) // 一次周期所有灯显示时间占比
		{
			*(u32 *)0x40010C0C &= ~0x2; // 蓝灯开 gb
			delay(blue);
			*(u32 *)0x40010C0C |= 0x2; // 蓝灯关 r

			*(u32 *)0x40010C0C &= ~0x20; // 红灯开
			delay(red);
			*(u32 *)0x40010C0C |= 0x20; // 红灯关	g

			*(u32 *)0x40010C0C &= ~1; // 绿灯开
			delay(green);
			*(u32 *)0x40010C0C |= 1; // 绿灯关
		}
		// 更改一次周期内各颜色显示时间占比
		if (blue == 100)
		{
			addb = -1;
			addr = 1;
			addg = 0;
		}
		else if (red == 100)
		{
			addb = 0;
			addr = -1;
			addg = 1;
		}
		else if (green == 100)
		{
			addb = 1;
			addr = 0;
			addg = -1;
		}

		blue += addb;
		red += addr;
		green += addg;
	}
#endif

// 三色切换显示
#if 0
	*(u32 *)0x4002 1018 |= 1 << 3; //

	*(u32 *)0x40010C00 &= ~0x00f000ff; // 将对应的开关设置制0
	*(u32 *)0x40010C00 |= 0x00100011;  // 将GPIO0，1，5对应的位置设置为推挽输出

	while (1)
	{
		*(u32 *)0x40010C0C &= ~1; // 绿灯开
		delay(50000);
		*(u32 *)0x40010C0C |= 1; // 绿灯关
		delay(50000);
		*(u32 *)0x40010C0C &= ~0x2; // 蓝灯开
		delay(50000);
		*(u32 *)0x40010C0C |= 0x2; // 蓝灯关
		delay(50000);
		*(u32 *)0x40010C0C &= ~0x20; // 红灯开
		delay(50000);
		*(u32 *)0x40010C0C |= 0x20; // 红灯关
		delay(50000);
	}
#endif

// 两两混合显示
#if 0
	*(u32 *)0x4002 1018 |= 1 << 3; //

	*(u32 *)0x40010C00 &= ~0x00f000ff; // 将对应的开关设置制0
	*(u32 *)0x40010C00 |= 0x00100011;  // 将GPIO0，1，5对应的位置设置为 推挽 输出

	while (1)
	{
		*(u32 *)0x40010C0C &= ~1; // 绿灯开  rg
		delay(500000);
		*(u32 *)0x40010C0C |= 0x20; // 红灯关	g
		delay(500000);
		*(u32 *)0x40010C0C &= ~0x2; // 蓝灯开 	gb
		delay(500000);
		*(u32 *)0x40010C0C |= 1; // 绿灯关	b
		delay(500000);
		*(u32 *)0x40010C0C &= ~0x20; // 红灯开	br
		delay(500000);
		*(u32 *)0x40010C0C |= 0x2; // 蓝灯关	r
		delay(500000);
	}
#endif

// 按键切换蜂鸣器的开启关闭
#if 0
	int flags = 0;
	*(u32 *)0x4002 1018 |= 1 << 2; // 开启PA时钟使能
	// 设置按键
	*(u32 *)0x40010800 &= ~0xf;
	*(u32 *)0x40010800 |= 0x4;
	// 设置蜂鸣器
	*(u32 *)0x40010804 &= ~0xf;
	*(u32 *)0x40010804 |= 0x1;

	while (1)
	{
		if ((*(u32 *)0x40010808 & 0x1) == 1)
		{
			delay(200);
			while ((*(u32 *)0x40010808 & 0x1) == 1)
				;
			delay(200);
			flags = ~flags;
		}

		if (flags == 0)
		{
			*(u32 *)0x4001080c &= ~(0x1 << 8); // 蜂鸣器开启
											   // delay(50000);
		}
		else
		{
			*(u32 *)0x4001080C |= (0x1 << 8); // 蜂鸣器关闭
											  // delay(50000);
		}
	}

#endif

// 按键切换灯的颜色
#if 0
	int flags = 0;
	*(u32 *)0x4002 1018 |= 1 << 2; // 开启PA时钟使能
	*(u32 *)0x4002 1018 |= 1 << 3; // 开启PB时钟使能
	// 设置按键
	*(u32 *)0x40010800 &= ~0xf;
	*(u32 *)0x40010800 |= 0x4;
	// 设置三色灯
	*(u32 *)0x40010c00 &= ~0x00f000ff;
	*(u32 *)0x40010c00 |= 0x00100011; // 0x23

	*(u32 *)GPIO_BO |= 0x23; // 所有灯关闭

	while (1)
	{
		if ((*(u32 *)0x40010808 & 0x1) == 1)
		{
			delay(200);
			while ((*(u32 *)0x40010808 & 0x1) == 1)
				;
			delay(200);
			flags++;
			if (flags == 3)
			{
				flags = 0;
			}
		}

		switch (flags)
		{
		case 0:
			*(u32 *)0x40010C0C |= 0x20; // 红灯关
			*(u32 *)0x40010C0C |= 0x2;	 // 蓝灯关
			*(u32 *)0x40010C0C &= ~1;	 // 绿灯开
			delay(500000);
			break;
		case 1:
			*(u32 *)0x40010C0C |= 0x20; // 红灯关
			*(u32 *)0x40010C0C |= 1;	 // 绿灯关
			*(u32 *)0x40010C0C &= ~0x2; // 蓝灯开
			delay(500000);
			break;
		case 2:
			*(u32 *)0x40010C0C |= 1;	  // 绿灯关
			*(u32 *)0x40010C0C |= 0x2;	  // 蓝灯关
			*(u32 *)0x40010C0C &= ~0x20; // 红灯开
			delay(500000);
			break;
		}
	}

#endif
}
// 延时函数
void delay(volatile u32 n)
{
	while (n--)
		;
}

void SystemInit(void)
{
}
